<template>
  <!--  OJTBZ-EEUHH-AL4DC-WKT2G-UFIRV-DBBAO-->
  <div class="box">
    <div id="container" v-if="isShow || pressureStore"></div>
  </div>
</template>
<script lang="ts" setup>
import {nextTick, onMounted, ref, watch} from 'vue'
import {usePressureStore} from "../../../store/pressure";
const TMap = (window as any).TMap
const pressureStore = usePressureStore()
let coordinate = ref<Array<number>>([2, 10])
let region = ref<string>('')
let map:any;
function initMap() {
  if (pressureStore.pressureObj) {
    coordinate.value = pressureStore.pressureObj.coordinate
    region.value = pressureStore.pressureObj.region
  }
  let center = new TMap.LatLng(coordinate.value[0], coordinate.value[1]);//设置中心点坐标
  //初始化地图
  map = new TMap.Map("container", {
    center: center
  });

  //初始化infoWindow
  var infoWindow = new TMap.InfoWindow({
    map: map,
    position: center, //设置信息框位置
    content: region.value //设置信息框内容
  });
  watch(pressureStore, () => {
    if (pressureStore.pressureObj) {
      region.value = pressureStore.pressureObj.region
      infoWindow.setContent(region.value);
    }
  })
}

let isShow = ref<boolean>(false)
onMounted(() => {
  initMap()
  watch(pressureStore, () => {
    if(pressureStore.pressureObj){
      let [lat,lng] = pressureStore.pressureObj.coordinate
      map.setCenter(new TMap.LatLng(lat,lng));
      map.panTo([lat, lng], 2000)
      map.setZoom(18)
    }
    nextTick(() => {
      isShow.value = true
    })
  }, {deep: true, immediate: true})
})

</script>
<style lang="scss" scoped>
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

#container {
  width: 100%;
  height: 100%;
}

.color {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1001;
  display: flex;
  align-items: center;
}

.green {
  width: 40px;
  height: 40px;
  background: #00ff00;
  cursor: pointer;
}

.blue {
  width: 40px;
  height: 40px;
  background: #00ffff;
  cursor: pointer;
}

.box {
  margin-left: 10px;
  width: 814px;
  height: 570px;
  border: 5px solid #ebf5ff;
  padding: 10px;
}
</style>
